package com.wing.cms.ext.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.cms.domain.Template;
import com.wing.cms.ext.dao.CmsFunctionDao;
import com.wing.cms.ext.domain.CmsFunction;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Auth;
import com.wing.sys.service.AuthManager;
/**
 * 
 * @author djl
 * 
 */
@Service
@Transactional
public class CmsFunctionManager {
	@Autowired
	private CmsFunctionDao cmsFunctiondao;
	@Autowired
	private AuthManager authManager;
	
	
	
	public PageBean findPageList(int currentPage, int pageSize,String siteId){
		return cmsFunctiondao.findPageList(currentPage,pageSize,siteId);	 
	}
	
	/**
	 * 通过id得到对象
	 *
	 * @param  id
	 * @return pObject
	 */
	public CmsFunction findById(String id){
		return cmsFunctiondao.findById(id);	 
	}
	/**
	 * 通过fun_code得到对象
	 *
	 * @param  fun_code
	 * @return pObject
	 */
	public Map findByFun_code(String fun_code){
		return cmsFunctiondao.findByFun_code(fun_code);	 
	}
	
	
	/**
	 * 保存对象
	 *
	 * @param  fun
	 */
	public void saveCmsFunction(CmsFunction fun,String siteId){
		if(fun.getId() == null ||  "".equals(fun.getId())){
			fun.setId(new GUID().toString());
			cmsFunctiondao.insert(fun);
			Auth auth = new Auth();
			auth.setId(new GUID().toString());
			auth.setType(Auth.TYPE_FUNCTION);
			auth.setRes_id(fun.getId());
			auth.setOwner_id(siteId);
			authManager.insert(auth);
		}else{
			cmsFunctiondao.update(fun);
		}
	}

	/**
	 * 编辑对象
	 *
	 * @param  fun
	 */
	public void updateCmsFunction(CmsFunction fun){
		cmsFunctiondao.update(fun);
	}
	
	 /**
	 * 删除对象
	 *
	 * @param  chk 对象id数组
	 */
	public void deleteCmsFunctions(String[] chk) {
		for(int i=0;i<chk.length;i++){
			cmsFunctiondao.delete(chk[i]);
			authManager.deleteAuthsByTypeResId(Auth.TYPE_FUNCTION, chk[i]);
		}
	}

	public List getFunctionsBySiteId(String siteId) {
		// TODO Auto-generated method stub
		return cmsFunctiondao.getFunctionsBySiteId(siteId);
	}
}



